package com.atguigu.e_objectstream;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class Demo1 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //把存在文件中的对象读取到程序中[内存] -> 反序列化操作
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("T_Day23\\test\\obj.txt"));

        //因为写对象时,只写一次 --> 读的时候读一次 确定是ArrayList集合
        ArrayList<Object> list = (ArrayList<Object>) ois.readObject();

        for (Object obj : list) {
            System.out.println("obj = " + obj);
        }

        ois.close();
    }

    private static void extracted1(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        try{
            while (true){
                Object obj = ois.readObject();
                System.out.println("obj = " + obj);
            }
        }catch (EOFException e){
            //System.out.println("读到文件末尾了!");
        }
    }

    private static void extracted(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        //读对象
        Object obj = ois.readObject();
        System.out.println("obj = " + obj);
        obj = ois.readObject();
        System.out.println("obj = " + obj);
        obj = ois.readObject();
        System.out.println("obj = " + obj);
        obj = ois.readObject();
        System.out.println("obj = " + obj);

        System.out.println(ois.readObject());
        System.out.println(ois.readObject());
        System.out.println(ois.readObject());
        System.out.println(ois.readObject());
        System.out.println(ois.readObject());
        System.out.println(ois.readObject());
    }
}
